﻿using System;

/*
 *  author: able
 *  @2012/2/21 16:12:26
 * 
 */
namespace __ts.Core.Session
{
    /// <summary>
    /// 
    /// </summary>
    public class AttributeKey<T>
    {
        /** the {@link Class} of the referenced attribute-value */
        private T attributeType;

        /** the name of this key */
        private String attributeName;

        /** the cached hash code of this instance */
        private int _hashCode;

        /**
         * Creates a new {@link AttributeKey} with the given parameters. A
         * {@link IllegalArgumentException} will be thrown if any parameter is
         * <code>null</code>.
         * 
         * @param attributeType
         *            type of the referenced attribute-value, must not be
         *            <code>null</code>
         * @param attributeName
         *            name of this key, must not be <code>null</code>
         * @exception IllegalArgumentException
         *                if any parameter is <code>null</code>
         * @see #createKey(Class, String)
         */
        public AttributeKey(T attributeType, String attributeName)
        {
            //this.attributeType = assertNotNull(attributeType, "attributeType");
            //this.attributeName = assertNotNull(attributeName, "attributeName");

            this._hashCode = createHashCode();
        }

        /**
         * Creates a new {@link AttributeKey} with the given parameters. A
         * {@link IllegalArgumentException} will be thrown if any parameter is
         * <code>null</code>. <br>
         * This call is equal to {@link AttributeKey#AttributeKey(Class, String)}
         * 
         * @param attributeType
         *            type of the referenced attribute-value, must not be
         *            <code>null</code>
         * @param attributeName
         *            name of this key, must not be <code>null</code>
         * @exception IllegalArgumentException
         *                if any parameter is <code>null</code>
         * @see #AttributeKey(Class, String)
         */
        public static AttributeKey<T> createKey(T attributeType, String attributeName)
        {
            return new AttributeKey<T>(attributeType, attributeName);
        }

        /**
         * Creates the hash code for this instance
         * 
         * @return
         */
        private int createHashCode()
        {
            int prime = 31;
            int result = prime + attributeName.GetHashCode();
            result = prime * result + attributeType.GetHashCode();

            return result;
        }

        /**
         * Returns the name of this key.
         * 
         * @return name of this key, never <code>null</code>
         */
        public String getName()
        {
            return attributeName;
        }

        /**
         * Returns the type of this key.
         * 
         * @return type of this key, never <code>null</code>
         */
        public T getType()
        {
            return attributeType;
        }

        public int hashCode()
        {
            return _hashCode;
        }

        public bool equals(Object obj)
        {
            if (this == obj)
            {
                return true;
            }

            if (obj == null)
            {
                return false;
            }

            //if (getClass() != obj.getClass()) {
            //    return false;
            //}

            //AttributeKey<?> other = (AttributeKey<?>) obj;

            //return hashCode == other.hashCode;
            return false;
        }
    }
}